<?php

class SNAPEvent
{
	private $EventID;
	private $OriginalRecordID;
	private $CreatedWhen;
	private $CreatedBy;
	private $DeletedWhen;
	private $DeletedBy;
	private $Live;
	private $Start;
	private $End;
	private $Title;
	private $Description;
	private $Comment;
	private $Responsible;
	private $Involvees = array();
	private $Locations = array();
	private $TypeID = 0;

	
	public function setEventID($id){$this->EventID=$EventID=$id;}
	public function setOriginalRecordID($id){$this->OriginalRecordID=$id;}
	public function setCreatedWhen($date){$this->CreatedWhen=$date;}
	public function setCreatedBy($user){$this->CreatedBy=$user;}
	public function setDeletedWhen($date){$this->DeletedWhen=$date;}
	public function setDeletedBy($user){$this->DeletedBy=$user;}
	public function setLive($live){$this->Live=$live;}
	public function setStart($Start){$this->Start=$Start;}
	public function setEnd($End){$this->End=$End;}
	public function setTitle($Title){$this->Title=$Title;}
	public function setDescription($description){$this->Description=$description;}
	public function setComment($comment){$this->Comment=$comment;}
	public function setResponsible($responsible){$this->Responsible=$responsible;}
	public function setInvolvees($involvees){$this->Involvees=$involvees;}
	public function setLocations($locations){$this->Locations=$locations;}
	public function setTypeID($eventtypeid){$this->TypeID=$eventtypeid;}
	
	public function getEventID(){return $this->EventID;}
	public function getOriginalRecordID(){return $this->OriginalRecordID;}
	public function getCreatedWhen(){return $this->CreatedWhen;}
	public function getCreatedBy(){return $this->CreatedBy;}
	public function getDeletedWhen(){return $this->DeletedWhen;}
	public function getDeletedBy(){return $this->DeletedBy;}
	public function getLive(){return $this->Live;}
	public function getStart(){return $this->Start;}
	public function getEnd(){return $this->End;}
	public function getTitle(){return $this->Title;}
	public function getDescription(){return $this->Description;}
	public function getComment(){return $this->Comment;}
	public function getResponsible(){return $this->Responsible;}
	public function getInvolvees(){return $this->Involvees;}
	public function getLocations(){return $this->Locations;}
	public function getTypeID(){return $this->TypeID;}
	
	public function advanceStart($interval)
	{
		$start = $this->getStart();
		if($interval=="day")$start->add(new DateInterval('P1D'));
		elseif($interval=="week")$start->add(new DateInterval('P1W'));
		elseif($interval=="month")$start->add(new DateInterval('P1M'));
		$this->setStart($start->format("Y-m-d H:i"));
	}
	
	public function advanceEnd($interval)
	{
		$end = $this->getEnd();
		if($interval=="day")$end->add(new DateInterval('P1D'));
		elseif($interval=="week")$end->add(new DateInterval('P1W'));
		elseif($interval=="month")$end->add(new DateInterval('P1M'));
		$this->setEnd($end->format("Y-m-d H:i"));
	}
	
	public function getInvolveesList()
	{
		$html = "";
		foreach($this->Involvees as $involvee)
		{
			$html .= $GLOBALS['INVOLVEES'][$involvee]['LongName']."<br>";
		}
		$html = substr($html,0,-4);
		return $html;
	}

	public function getLocationsList()
	{
		$html = "";
		foreach($this->Locations as $location)
		{
			$html .= $GLOBALS['LOCATIONS'][$location]['LongName']."<br>";
		}
		$html = substr($html,0,-4);#
		return $html;
	}

	
	public function getLocationsShortList()
	{
		$html = "";
		foreach($this->Locations as $location)
		{
			$html .= $GLOBALS['LOCATIONS'][$location]['ShortName'].",";
		}
		$html = substr($html,0,-1);
		return $html;
	}

	
	public function __construct($EventID)
	{
		if($EventID==-1)
		{
			if(isset($_POST['Date'])) // when creating a new event to edit - not when inserting an event to database
			{
				$timeNow = new DateTime("now");
				$start = $timeNow->format("H:i");
				$timeNow->modify("+1 hour");
				$end = $timeNow->format("H:i");
				$newStartDateTime = new DateTime($_POST['Date']." ".$start);
				$newEndDateTime = new DateTime($_POST['Date']." ".$end);
				$this->Start=$newStartDateTime;
				$this->End=$newEndDateTime;
			}
			return; //create an empty event
		}
		
		$database=$GLOBALS['DATABASE']->Recopy();
		$sql="SELECT E.*,ET.EventType FROM Events as E INNER JOIN EventTypes as ET On E.EventTypeID=ET.EventTypeID WHERE E.EventID='".$EventID."' ";
//		echo $sql;
		$database->executeSQL($sql);
		$row=$database->getRow();
		$this->EventID=$row['EventID'];
		$this->OriginalRecordID=$row['OriginalRecordID'];
		$this->CreatedWhen=new DateTime($row['CreatedWhen']);
		$this->CreatedBy=$row['CreatedBy'];
		$this->DeletedWhen=new DateTime($row['DeletedWhen']);
		$this->DeletedBy=$row['DeletedBy'];
		$this->Live=$row['Live'];
		$this->Start=new DateTime($row['Start']);
		$this->End=new DateTime($row['End']);
//		echo $this->Start;
//		echo $this->End;
		$this->Title=$row['Title'];
		$this->Description=$row['Description'];
		$this->Comment=$row['Comment'];
		$this->Responsible=$row['Responsible'];
		$this->TypeID=$row['EventTypeID'];
		
		
// locations
		$sql = "SELECT LocationNameID, EventId FROM Locations WHERE EventID = ".$this->EventID;
		$database->ExecuteSQL($sql);
		$result = array();
		while($row = $database->getRow())
		{
			array_push($result,$row['LocationNameID']);
		}
		$this->Locations = $result;
		
// Involvees
		$sql = "SELECT EventInvolveeNameID, EventId FROM EventInvolvees WHERE EventID = ".$this->EventID;
		$database->ExecuteSQL($sql);
		$result = array();
		while($row = $database->getRow())
		{
			array_push($result,$row['EventInvolveeNameID']);
		}
		$this->Involvees = $result;
	}
	
	
	public function loadFromPOST()
	{
		$this->EventID = $_POST['eventid'];
		$this->OriginalRecordID = $_POST['originalrecordid'];
		$this->Start = $_POST['date']." ".$_POST['start'];
		$this->End = $_POST['date']." ".$_POST['end'];
		$this->Title = $_POST['title'];
		$this->Description = $_POST['description'];
		$this->Comment = $_POST['comment'];
		$this->Responsible = $_POST['responsible'];
		$this->TypeID = $_POST['eventtype'];
		$this->Involvees = $_POST['involvees'];
		$this->Locations = $_POST['locations'];
	}
	
	public function delete()
	{
		$database=$GLOBALS['DATABASE']->Recopy();
		$timeNow = new DateTime("now");
		$sql = "UPDATE Events SET DeletedWhen = '".$timeNow->format("Y-m-d H:i")."', DeletedBy = '".$GLOBALS['USER_ID']."' ";
		$sql .= " WHERE EventID = ".$this->EventID;
//		echo $sql."<br>\n";
		$database->ExecuteSQL($sql);
		
	}

	private function insert()
	{
		$dateNow = new DateTime("now");
		$database=$GLOBALS['DATABASE']->Recopy();
		$sql = "\nINSERT INTO Events (CreatedWhen,CreatedBy,OriginalRecordID,Live,Start,End,Title,Description,Comment,Responsible,EventTypeID) ";
		$sql .= "VALUES('".$dateNow->format("Y-m-d H:i")."','".$GLOBALS['USER_ID']."','$this->OriginalRecordID','$this->Live','$this->Start','$this->End','".addslashes($this->Title)."','".addslashes($this->Description)."','".addslashes($this->Comment)."','".addslashes($this->Responsible)."','$this->TypeID')";
		echo $sql."<br>\n";
		$database->ExecuteSQL($sql);
		$newEventID = mysql_insert_id();
		foreach($this->Involvees as $involvee)
		{
			$sql = "INSERT INTO EventInvolvees (EventID,EventInvolveeNameID) VALUES ('".$newEventID."','".$involvee."')";
			echo $sql."<br>\n";
			$database->ExecuteSQL($sql);
		}
		foreach($this->Locations as $location)
		{
			$sql = "INSERT INTO Locations (EventID,LocationNameID) VALUES ('".$newEventID."','".$location."')";
			echo $sql."<br>\n";
			$database->ExecuteSQL($sql);
		}

// if old event was referenced in musiclist, create a new music list
		$sql = "SELECT MusicListID, SNAPEventID FROM MusicAdmin_MusicList WHERE SNAPEventID=".$this->EventID;
		$database->ExecuteSQL($sql);
		if($row = $database->getRow())
		{
			$sql = "INSERT INTO MusicAdmin_MusicList ";
			$sql .= "(SNAPEventId,Deleted,Live,Date,Time,Choir,Note,Service,Psalms,Hymns,Introit,Responses,Mass,Canticles,Anthem)  ";
			$sql .= "SELECT ".$newEventID.",Deleted,Live,Date,Time,Choir,Note,Service,Psalms,Hymns,Introit,Responses,Mass,Canticles,Anthem	 ";					
			$sql .= "FROM MusicAdmin_MusicList ";
			$sql .= "WHERE MusicListID = ".$row['MusicListID'];
			echo $sql."<br>\n";
			$database->ExecuteSQL($sql);
		}


		return $newEventID;
	}
	
	public function insertAsNew() // returns new EventID
	{
		return $this->insert();
	}
	
	public function update() // returns new EventID
	{
		if($this->OriginalRecordID==0)$this->OriginalRecordID=$this->EventID;
		$newID = $this->insert();
		$this->delete();
		$this->__construct($newID);
		return $newID;
	}
	
}
